package com.alibaba.algo.controller.user;

import com.alibaba.algo.annotation.OperateLogAnnotation;
import com.alibaba.algo.apiimpl.user.SysEmployeeServiceImpl;
import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.common.PagerEntity;
import com.alibaba.algo.cooperation.usercenter.GlobalResultStatus;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.*;
import com.alibaba.algo.enums.OperateTypeEnum;
import com.alibaba.algo.enums.ResourceTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 类SysEmployeeController.java的实现描述：员工 Controller
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@RestController
@RequestMapping(value = "/usercenter-admin-app/admin/sysEmployee")
@Slf4j
@Api(value = "员工管理", tags = "员工管理")
public class SysEmployeeController {

    @Autowired
    private SysEmployeeServiceImpl sysEmployeeService;

    /**
     * 新增员工
     *
     * @param vo
     * @return
     */
    @PostMapping("/insert")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.EMPLOYEE,operateType = OperateTypeEnum.INSERT)
    public BaseResult insert(@RequestBody SaveEmployeeRequestVO vo) {
        if (StringUtils.isEmpty(vo.getEmployeeCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_EMPLOYEE_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_EMPLOYEE_CODE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getEmployeeName())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_EMPLOYEE_NAME_ERROR.getCode(), GlobalResultStatus.PARAM_EMPLOYEE_NAME_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getEmployeeType())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_EMPLOYEE_TYPE_ERROR.getCode(), GlobalResultStatus.PARAM_EMPLOYEE_TYPE_ERROR.getMsg());
        }
//        if (StringUtils.isEmpty(vo.getMobilePhone())) {
//            return BaseResult.buildError(GlobalResultStatus.PARAM_EMPLOYEE_MOBILE_PHONE_ERROR.getCode(), GlobalResultStatus.PARAM_EMPLOYEE_MOBILE_PHONE_ERROR.getMsg());
//        }
        return sysEmployeeService.insert(vo);
    }

    /**
     * 修改员工
     *
     * @param vo
     * @return
     */
    @PostMapping("/update")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.EMPLOYEE,operateType = OperateTypeEnum.UPDATE)
    public BaseResult update(@RequestBody UpdateEmployeeRequestVO vo) {
        if (StringUtils.isEmpty(vo.getId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_EMPLOYEE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_EMPLOYEE_ID_ERROR.getMsg());
        }
        return sysEmployeeService.update(vo);
    }

    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @PostMapping("/selectPage")
    @ApiOperation(value = "员工管理分页查询")
    public BaseResult selectList(@RequestBody SelectEmployeeListRequestVO vo) {
        if (StringUtils.isEmpty(vo.getPageIndex())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_PAGE_INDEX_ERROR.getCode(), GlobalResultStatus.PARAM_PAGE_INDEX_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getPageSize())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_PAGE_SIZE_ERROR.getCode(), GlobalResultStatus.PARAM_PAGE_SIZE_ERROR.getMsg());
        }
        PagerEntity pagerEntity = sysEmployeeService.selectPage(vo);
        return BaseResult.buildSuccess(pagerEntity);
    }

    /**
     * 删除员工
     *
     * @param employeeIdList 员工列表
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "员工管理逻辑删除")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.EMPLOYEE,operateType = OperateTypeEnum.DELETE)
    public BaseResult delete(@RequestBody List<Long> employeeIdList) {
        if (CollectionUtils.isEmpty(employeeIdList)) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_EMPLOYEE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_EMPLOYEE_ID_ERROR.getMsg());
        }
        return sysEmployeeService.delete(employeeIdList);
    }

    @PostMapping("/revalidate")
    @ApiOperation(value = "员工管理复效")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.EMPLOYEE,operateType = OperateTypeEnum.DELETE)
    public BaseResult revalidate(@RequestBody List<Long> employeeIdList) {
        if (CollectionUtils.isEmpty(employeeIdList)) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_EMPLOYEE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_EMPLOYEE_ID_ERROR.getMsg());
        }
        return sysEmployeeService.revalidate(employeeIdList);
    }

    /**
     * 查询当前员工的组织和岗位列表
     *
     * @param vo 入参
     * @return
     */
    @PostMapping("/selectOrgAndPosition")
    public BaseResult selectDetail(@RequestBody SelectEmployeeDetailRequestVO vo) {
        if (StringUtils.isEmpty(vo.getEmployeeId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_EMPLOYEE_ID_ERROR.getCode(), GlobalResultStatus.PARAM_EMPLOYEE_ID_ERROR.getMsg());
        }
        return sysEmployeeService.qryOrgAndPosition(vo);
    }

    /**
     * 删除当前员工的组织和岗位关联关系
     *
     * @param deleteOrgAndPositionReqVOList 入参
     * @return
     */
    @PostMapping("orgAndPosition/delete")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.EMPLOYEE,operateType = OperateTypeEnum.DELETE)
    public BaseResult selectDetail(@RequestBody List<DeleteOrgAndPositionReqVO> deleteOrgAndPositionReqVOList) {
        return sysEmployeeService.deleteOrgAndPosition(deleteOrgAndPositionReqVOList);
    }


    /**
     * 批量导入员工数据
     *
     * @param file 入参
     * @return
     */
    @PostMapping("import")
    @ApiOperation(value = "批量导入员工")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.EMPLOYEE,operateType = OperateTypeEnum.INSERT)
    public BaseResult importBatch(MultipartFile file) {
       return sysEmployeeService.importBatch(file);
    }

}
